package cuit.campus.taskRunning.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cuit.campus.taskRunning.user.entry.dto.UserDto;
import cuit.campus.taskRunning.user.entry.po.User;
import cuit.campus.taskRunning.user.serivce.UserService;
import cuit.campus.taskRunning.commons.response.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody UserDto userDto)
    {
        return userService.login(userDto);
    }
    @GetMapping("/login/phone")
    public Result login(@RequestParam("phone") String phone,@RequestParam("code") String code)
    {
        return userService.login(phone,code);
    }
    @GetMapping("/register/isExist")
    public Result isExist(@RequestParam("username") String username){
        var wrapper = new LambdaQueryWrapper<User>().eq(User::getUsername, username);
        boolean exists = userService.exists(wrapper);
        if(!exists)
            return Result.ok("用户名不存在");
        else
            return Result.fail("用户名已存在");
    }
    @PostMapping("/register/send")
    public Result sendSend(@RequestBody UserDto userDto){
        return userService.send(userDto);
    }

    @PostMapping("/register")
    public Result save(@RequestBody UserDto userDto){
        log.info("userDto:{}",userDto);
        boolean save = userService.save(userDto);
        if(save)
            return Result.ok(userDto,"注册成功");
        else
            return Result.fail("注册失败");
    }

    @PostMapping("/userinfo")
    public Result getUserInfo(){
        return Result.ok("注册失败");
    }
}
